package com.getpebble.android.common.framework.util;

import android.content.Context;
import com.getpebble.android.common.core.trace.Trace;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipInputStream;

public class AssetUtil {
    public static final String TAG = AssetUtil.class.getSimpleName();

    public static InputStream getFileAsInputStream(Context context, String fileName) throws IllegalArgumentException, IOException {
        if (context == null) {
            throw new IllegalArgumentException("'context' may not be null!");
        } else if (fileName != null && fileName.length() >= 1) {
            return context.getAssets().open(fileName);
        } else {
            throw new IllegalArgumentException("'fileName' may not be null or empty!");
        }
    }

    public static String getFileAsString(Context context, String fileName) throws IllegalArgumentException, IOException {
        return getFileAsString(context, fileName, false);
    }

    public static String getFileAsString(Context context, String fileName, boolean isZipFile) throws IllegalArgumentException, IOException {
        IOException e;
        Throwable th;
        BufferedReader bufferedReader = null;
        StringBuilder sb = new StringBuilder();
        try {
            InputStream stream = getFileAsInputStream(context, fileName);
            if (isZipFile) {
                InputStream zipStream = new ZipInputStream(stream);
                zipStream.getNextEntry();
                stream = zipStream;
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(stream));
            while (true) {
                try {
                    String line = br.readLine();
                    if (line == null) {
                        break;
                    }
                    sb.append(line);
                } catch (IOException e2) {
                    e = e2;
                    bufferedReader = br;
                } catch (Throwable th2) {
                    th = th2;
                    bufferedReader = br;
                }
            }
            if (br != null) {
                try {
                    br.close();
                    bufferedReader = br;
                } catch (IOException e3) {
                    Trace.error(TAG, "Unable to close buffered stream!", e3);
                    bufferedReader = br;
                }
            }
        } catch (IOException e4) {
            e3 = e4;
            try {
                Trace.error(TAG, "Unable to read buffered data!", e3);
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e32) {
                        Trace.error(TAG, "Unable to close buffered stream!", e32);
                    }
                }
                return sb.toString();
            } catch (Throwable th3) {
                th = th3;
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e322) {
                        Trace.error(TAG, "Unable to close buffered stream!", e322);
                    }
                }
                throw th;
            }
        }
        return sb.toString();
    }
}
